package com.lzy.product.common.util;

import java.util.regex.Pattern;

/**
 * 正则工具类
 */
public final class RegularUtil {


    /**
     * 校验身份证号码
     *
     * @param idNumber
     * @return
     */
    public static Boolean checkIdNumer(String idNumber) throws Exception {
        String pattern = "";
        if (null == idNumber || idNumber.equals("")) {
            throw new Exception("身份证号码为空！");
        } else if (idNumber.length() == 15) {
            pattern = "^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}[0-9Xx]$";
        } else if (idNumber.length() == 18) {
            pattern = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        } else {
            throw new Exception("身份证号码长度非法！");
        }
        Pattern idNumberPattern = Pattern.compile(pattern);
        return idNumber.matches(idNumber);
    }

}
